﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FAST
{
    /// <summary>
    /// Maintains a List of FolderPairItem to keep track of different folder pair.
    /// </summary>
    [Serializable()]
    public class FolderPairHandler
    {
        private string loadedFolderPairName;
        private bool isFirstCreated;
        private Dictionary<string, FolderPairItem> folderpairs;

        public FolderPairHandler()
        {
            isFirstCreated = true;
            loadedFolderPairName = null;
            folderpairs = new Dictionary<string, FolderPairItem>();
        }


        /// <summary>
        /// Check if user input name already exist in folderPairs. folderPairs serves as a database to store folder pair names.
        /// </summary>
        /// <param name="name">User input folder pair name</param>
        /// <returns></returns>
        public bool isFolderPairNameUnique(string name)
        {
            
            bool isUnique;
            if (!name.Equals(""))
            {
               
                name = name.ToUpper();
                if (this.folderpairs.Count == 0)
                    isUnique = true;
                else
                    isUnique = !this.folderpairs.ContainsKey(name);
            }
            else
                isUnique = false;

            return isUnique;
        }


        public void resetLoadedFolderPair()
        {
            this.loadedFolderPairName = null;
        }
        

        /// <summary>
        /// Adds a new FolderPairItem to folderPairs and the Dictionary is serialized.
        /// </summary>
        ///<param name="fpName">The folder Pair name</param>
        public void createNewFolderPair(string fpName)
        {
            string path = Application.StartupPath;
            string diskLetter = path.Substring(0, path.IndexOf('\\') + 1);
           
            if (isFolderPairNameUnique(fpName))
            {
                if (isFirstCreated)
                {

                    Directory.CreateDirectory(diskLetter + @"\Sessions");

                    //Hide and Disallow user to access the storage file.
                    File.SetAttributes(diskLetter + @"\Sessions", FileAttributes.ReadOnly | FileAttributes.Hidden);
                    isFirstCreated = false;
                }
                fpName = fpName.ToUpper();
                Directory.CreateDirectory(diskLetter + @"\Sessions\Session_" + fpName);
                folderpairs.Add(fpName, new FolderPairItem(fpName));

                //set meta data
                this.loadedFolderPairName = fpName;
            }
        }


        /// <summary>
        /// Stores source, destination folder path and List<FileHandler> of leftFolderList and rightFolderList into a last loaded FolderItem. The leftFolderList and rightFolderList is than set to be serialized to be stored as an original state in a particular folder pair.
        /// </summary>
        /// <param name="src">The source path</param>
        /// <param name="des">The destination path</param>
        /// <param name="leftfilesList">The list of the left files</param>
        /// <param name="rightfilesList">The list of the right files</param>
        public void updateFolderPair(string src, string des, List<FilesHandler> leftfilesList, List<FilesHandler> rightfilesList)//parameter add version list in future
        {
            folderpairs[this.loadedFolderPairName].setUpdatedDate(DateTime.Now);
            folderpairs[this.loadedFolderPairName].setFolderPaths(src, des);

            OriginalState leftOS = new OriginalState(folderpairs[this.loadedFolderPairName].getNewOriginalStatePathLeft());
            leftOS.setOriginalState(leftfilesList);

            OriginalState rightOS = new OriginalState(folderpairs[this.loadedFolderPairName].getNewOriginalStatePathRight());
            rightOS.setOriginalState(rightfilesList);
        }

       

        /// <summary>
        /// Gets last loaded FolderPairItem. This object corresponds to the FolderPairItem of key loadedFolderPairName.
        /// </summary>
        /// <returns>Returns null if not last session</returns>
        public FolderPairItem getLastLoadedFolderPair()
        {
            if (this.loadedFolderPairName != null)
                return folderpairs[this.loadedFolderPairName];
            else return null;
        }
        


        /// <summary>
        /// Gets the last loaded folder pair log.
        /// </summary>
        /// <returns>Returns a string log of a message reporting meta data of the last loaded folder pair</returns>
        public string getLastLoadedFolderPairLog()
        {
            if (this.loadedFolderPairName != null)
            {
                return "You have a last previous folder pair created at " + folderpairs[this.loadedFolderPairName].getCreatedTimeStamp().ToString() + ". Updated at " + folderpairs[this.loadedFolderPairName].getLastUpdatedTimeStamp().ToString() + ".";
            }
            else
            {
                return "";
            }
        }


        public string getLastLoadedFolderPairName()
        {
            if (this.loadedFolderPairName != null)
            {
                return this.loadedFolderPairName;
            }
            else
            {
                return "";
            }
        }

        

        /// <summary>
        /// Removes the element at keyName from folderPairs. folderPairs is serialize to register the removal of the last loaded folder pair.
        /// </summary>
        /// <param name="keyName"></param>
        public void removeLastLoadedFolderPair(string keyName)
        {
            //try
            //{
                Directory.Delete(folderpairs[keyName].getFolderPairDirectoryStoragePath(), true);

                folderpairs.Remove(keyName);

                resetLoadedFolderPair();
            //}
            //catch (Exception)
            //{
            //}
        }
        

        /// <summary>
        /// Returns all folder pairs of the program
        /// </summary>
        /// <returns></returns>
        public Dictionary<string,FolderPairItem> getFolderPairs()
        {
            return this.folderpairs;
        }

        /// <summary>
        /// Returns the folder path stored in last loaded FolderPairItem.
        /// </summary>
        /// <returns></returns>
        public string getFolderPairLeftFolderPath()
        {
            if (this.loadedFolderPairName != null)
                return folderpairs[this.loadedFolderPairName].getLeftFolderPath();
            else
                return "";
        }


        /// <summary>
        /// Returns the folder path stored in last loaded FolderPairItem.
        /// </summary>
        /// <returns></returns>
        public string getFolderPairRightFolderPath()
        {
            if (this.loadedFolderPairName != null)
                return folderpairs[this.loadedFolderPairName].getRightFolderPath();
            else
                return "";
        }

        public void loadFolderPairByName(string name)
        {
            this.loadedFolderPairName = name;
        }
    }
}
